import Request from 'luch-request'
import { useStore } from '@/store'

const axios = new Request()

/* 设置全局配置 */
axios.setConfig((config) => {
	config.timeout = 6000
	// config.baseURL = 'http://localhost:8080/api/'
	config.baseURL = 'http://192.168.0.130:8080/api/'
	config.header = {
		...config.header
	}
	return config
})


axios.interceptors.request.use((config) => {
	const store = useStore()
	console.log('interceptors.request', store.token)
	config.header = {
		...config.header,
		Authorization: store.token
	}
	return config
}, (error) => {
	return Promise.reject(error)
})


axios.interceptors.response.use((response) => {
	console.log(response)
	return response.data
}, (error) => {
	if (error.errMsg && error.errMsg.indexOf('fail') !== -1) {
		console.log('请求失败')
		uni.showToast({
			icon:"none",
			title:'请求失败，请检查服务器',
			mask:true
		})
	}
	if (error.errMsg && error.errMsg.indexOf('timeout') !== -1) {
		console.log('请求超时')
		uni.showToast({
			icon:"none",
			title:'请求超时，请重试',
			mask:true
		})
	}
	return Promise.reject(error)
})

export {
	axios
}